var name,background,suffix;
window.onload=function(){
	background = chrome.extension.getBackgroundPage();
	document.getElementById("down").onclick = function(){	
		if(background.url){
			if(chrome.downloads.download){
                chrome.downloads.download(
                    {
                        url: background.url,
                        filename: "MUS/"+name+"."+suffix,
                        saveAs: true,
                        conflictAction: "uniquify"
                    }
                )
			}else{
				var a_dom = document.createElement('a');
				a_dom.href = background.url;
				a_dom.download = name+".mp3";
				a_dom.click();
			}
            document.getElementById("down").style.display = 'none';
            document.getElementById("title").style.display = 'none';
            document.getElementById("check").style.display = 'block';
            background.url=null;
		}
	}
	document.getElementById("check").onclick = function(){
		getCurrentTabId(function(tabId){
			chrome.tabs.executeScript(tabId, {file: '/js/content_scripts.js'});
		});
	}
}
document.getElementById("laya").onclick = function(){
    window.open("https://www.ximalaya.com/");
}
document.getElementById("sing").onclick = function(){
    window.open("https://changba.com/");
}
document.getElementById("qmkg-link").onclick = function(){
    window.open("http://kg.qq.com/");
}
document.getElementById("kougou-link").onclick = function(){
    window.open("http://www.kugou.com/");
}
document.getElementById("qq-link").onclick = function(){
	window.open("https://y.qq.com/");
}
document.getElementById("wy-link").onclick = function(){
	window.open("http://music.163.com/");
}
document.getElementById("kuwo-link").onclick = function(){
	window.open("http://www.kuwo.cn/");
}
document.getElementById("xiami-link").onclick = function(){
	window.open("https://www.xiami.com/");
}
document.getElementById("migu-link").onclick = function(){
	window.open("http://music.migu.cn/");
}
document.getElementById("update").onclick = function(){
	window.open("http://www.jianshu.com/users/574c887820a9/");
}
document.getElementById("help").onclick = function(){
	window.open("http://www.jianshu.com/p/9f8e07ee7708");
}
function getCurrentTabId(callback)
{
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs)
    {
        if(callback) callback(tabs.length ? tabs[0].id: null);
    });
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse)
{
	if(request.musicName && background.url){
  		name = (request.musicName).replace(/[?<>*"~:\|\/\\]/g,"+");
		var suffixArr = background.url.split('.');
		for(var i=0,len=suffixArr.length;i<len;i++){
			var temp = suffixArr[i].slice(0,3);
			if(temp == 'mp3' || temp=='m4a' || temp=='aac'){
				suffix = temp;
			}
		}
		suffix = suffix||"mp3";
  		document.getElementById("down").style.display = 'block';
  		document.getElementById("title").style.display = 'block';
  		document.getElementById("title").innerText = "曲名："+request.musicName;
  		document.getElementById("check").style.display = "none";
	}else {
		document.getElementById("title").style.display = 'block';
		document.getElementById("title").innerText = "识别失败，请刷新页面重试..."
	}
});